<?php

namespace app\storeapi\controller\apps\dealer;

use app\common\model\SettingLog as SettingLogSettingModel;
use app\store\model\Wxapp as WxappModel;
use app\storeapi\controller\Controller;
use app\storeapi\model\dealer\DistributionSetting as DistributionSettingModel;

/**
 * 分销设置
 * Class Setting
 * @package app\store\controller\apps\dealer
 */
class DistributionSetting extends Controller
{

    /**
     * 分销方案一详情
     * @param int $apply_status
     * @param int $page
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function oneCase()
    {
        $dealer_type = 1;
        //获取该商户的分销方案
        $dealerCase = WxappModel::dealerCase($this->wxapp_id);
        $param = $this->postData();
        $list = DistributionSettingModel::getItem($dealer_type,$param['wxapp_id']);
        if(empty($list)){
            $list['status'] = 2;//配置状态  1：开启 2：关闭
            $list['distribution_type'] = 1;//分销类型  1：单笔  2：累加
            $list['distribution_con'] = 0;//分销条件
            $list['consume_charge'] = 0;//消费赏金百分比
            $list['product_status'] = 2;//商品赏金  1：开启 2：关闭
            $list['card_status'] = 2;//储值卡赏金状态 1：开启  2：关
            $list['hot_sale_status'] = 2;//爆品礼赏金状态  1：开启 2：关闭
            $list['dealerCase'] = 0;
        }else{
            $list = json_decode($list['setting_content'],true);
            $list['dealerCase'] = $dealerCase;
        }
        return $this->renderSuccess($list);
    }



    /**
     * 分销方案二详情
     * @param int $apply_status
     * @param int $page
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function towCase()
    {
        $dealer_type = 2;
        //获取该商户的分销方案
        $dealerCase = WxappModel::dealerCase($this->wxapp_id);
        $param = $this->postData();
        $list = DistributionSettingModel::getItem($dealer_type,$param['wxapp_id']);

        if(empty($list)){
            $data = [
                'common'=>[
                    'dealer_percent'=>0,
                    'face_status'=>2,
                    'product_status'=>2,
                    'card_status'=>2,
                    'gifts_status'=>2,
                ],
                'staff'=>[
                    'dealer_percent'=>0,
                    'face_status'=>2,
                    'product_status'=>2,
                    'card_status'=>2,
                    'gifts_status'=>2,
                ],
                'wgd'=>[
                    'dealer_percent'=>0,
                    'face_status'=>2,
                    'product_status'=>2,
                    'card_status'=>2,
                    'gifts_status'=>2,
                ],
                'vip'=>[
                    'face_status'=>2,
                    'product_status'=>2,
                    'card_status'=>2,
                    'gifts_status'=>2,
                ],
                'dealerCase' => 0,
            ];
        }else{
            $data = json_decode($list['setting_content'],true);
            $data['dealerCase'] = $dealerCase;
        }
        return $this->renderSuccess($data);
    }




    /**
     * 编辑分销方案一
     * @param $category_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function setOne()
    {
        $dealer_type = 1;
        $param = $this->postData();
        //写入日志
        $settingLog = new SettingLogSettingModel();
        //设备类型
        $deviceType = getDeviceType();

        $logData = [
            'type'=>3,//1：分红设置  2：会员卡分红设置 3：分销方案一 4：分销方案二
            'create_time'=>time(),
            'client'=>"分销方案一",
            'deviceType'=>$deviceType,
            'wxapp_id'=>$param['wxapp_id'],
        ];

        $setting_content = [
            'distribution_con' => $param['distribution_con'],
            'distribution_type' => $param['distribution_type'],
            'consume_charge' => $param['consume_charge'],
            'product_status' => $param['product_status'],
            'card_status' => $param['card_status'],
            'hot_sale_status' => $param['hot_sale_status'],
            'status' => $param['status'],
            'dealerCase' => $param['dealerCase'],
        ];
        $model = DistributionSettingModel::getItem($dealer_type,$param['wxapp_id']);
        if(empty($model)){

            if ((new DistributionSettingModel)->setOne($param)) {
                $settingLog->addData($logData,$setting_content);
                return $this->renderSuccess([],"设置成功");
            }
            return $this->renderError($model->getError() ?: '设置失败');

        }

        if ($model->setOne($param)) {
            $settingLog->addData($logData,$setting_content);
            return $this->renderSuccess([],"设置成功");
        }

        return $this->renderError($model->getError() ?: '设置失败');

    }



    /**
     * 编辑分销方案二
     * @param $category_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function setTow()
    {

        $dealer_type = 2;
        $param = $this->postData();

        if(!isset($param['staff'])){
            $param['staff'] = [
                'dealer_percent'=>0,
                'face_status'=>2,
                'product_status'=>2,
                'card_status'=>2,
                'gifts_status'=>2,
            ];
        }

        $model = DistributionSettingModel::getItem($dealer_type,$param['wxapp_id']);

        //写入日志
        $settingLog = new SettingLogSettingModel();
        //设备类型
        $deviceType = getDeviceType();

        $logData = [
            'type'=>4,//1：分红设置  2：会员卡分红设置 3：分销方案一 4：分销方案二
            'create_time'=>time(),
            'client'=>"分销方案二",
            'deviceType'=>$deviceType,
            'wxapp_id'=>$param['wxapp_id'],
        ];


        if(empty($model)){

            if ( (new DistributionSettingModel)->setTow($param)) {
                $settingLog->addData($logData,$param);
                return $this->renderSuccess([],"设置成功");
            }
            return $this->renderError($model->getError() ?: '设置失败');

        }

        if ($model->setTow($param)) {
            $settingLog->addData($logData,$param);
            return $this->renderSuccess([],"设置成功");
        }

        return $this->renderError($model->getError() ?: '设置失败');

    }





}